/*
 * Created on 24/ago/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc;
//
import java.util.Vector;

import it.unibo.deis.lia.mmhc.pas.*;
import it.unibo.deis.lia.mmhc.rom.*;
import it.unibo.deis.lia.mmhc.util.*;

public class MACMain extends Thread {

	// private MACMain macMain=null;
	// private ConnectorManager connectorManager=ConnectorManager.getInstance();
	private RoutingManager routingManager = RoutingManager.getInstance();
	private PathSelector pathSelector = null;

	// private RoutingManager routingManager=RoutingManager.getInstance();

	/*
	 * public MACMain getInstance()throws Exception{ if(macMain==null){
	 * macMain=new MACMain(); } return macMain; }
	 */

	public MACMain() throws Exception {
		// channelSelector=new ChannelSelector(new SimpleTopMetric((float)0.6));
		this(new SimplePathMetric((float) 0.001));
	}

	public MACMain(PathMetric pathMetric) throws Exception {
		new PeerConnectorGUI().start();
		pathSelector = new PathSelector(pathMetric);
		RoutingManager.getInstance();

		Runtime.getRuntime().exec("iptables -F");
		Runtime.getRuntime().exec("iptables -t nat -F");
		Runtime.getRuntime().exec("iptables -t mangle -F");
		Runtime.getRuntime().exec("iptables -X");
		// Runtime.getRuntime().exec("echo \"1\" > /proc/sys/net/ipv4/ip_forward");
		Runtime.getRuntime().exec("sysctl -w net.ipv4.ip_forward=1");
		Runtime.getRuntime().exec("echo \"1\" > /proc/sys/net/ipv4/ip_dynaddr");
	}

	public void setTopMetric(PathMetric newPathMetric) {
		pathSelector.setMetric(newPathMetric);
	}

	public Path getBestPath() throws Exception {
		Path bestPath = pathSelector.getBestPath();
		return bestPath;
	}

	public Path getBestChannel(PathMetric pathMetric) throws Exception {
		Path bestPath = pathSelector.getBestPath(pathMetric);
		return bestPath;
	}

	public Vector<Path> getAvailablePaths() throws Exception {
		Vector<Path> availablePaths = routingManager.getAvailablePaths();
		return availablePaths;
	}

	/*
	 * public Vector<Connector> getSuitableConnectors()throws Exception{
	 * Vector<Connector>
	 * suitableConnectors=connectorManager.getSuitableConnectors(); return
	 * suitableConnectors; }
	 */
}
